package cuit.demo.controller;

import cuit.demo.Entity.Article;
import cuit.demo.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SearchController {

    @Autowired
    private MyService myService;

    @GetMapping("/api/search")
    public ResponseEntity<List<Article>> searchArticleByTitle(@RequestParam String title) {
        List<Article> articles = myService.findArticlesByTitleOrAuthor(title);
        if (articles.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(articles, HttpStatus.OK);
    }
}